﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq.Expressions;
using System.Xml.Linq;
using curbside.Extensions;
using System.Configuration;
using System.Text;
using System.IO;
using System.Linq;
using curbside.IBatis;
using System.Collections;

namespace TestConsoleApplication
{
    public class Product
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

    public class ProductIdComparer : IEqualityComparer<Product>
    {
        public bool Equals(Product x, Product y)
        {
            if (x == null)
                return y == null;
            return x.Id == y.Id;
        }

        public int GetHashCode(Product obj)
        {
            if (obj == null)
                return 0;
            return obj.Id.GetHashCode();
        }
    }
    public class PropertyComparer<T> : IEqualityComparer<T>
    {
        private PropertyInfo _PropertyInfo;

        /// <summary>
        /// 通过propertyName 获取PropertyInfo对象    
        /// </summary>
        /// <param name="propertyName"></param>
        public PropertyComparer(string propertyName)
        {
            _PropertyInfo = typeof(T).GetProperty(propertyName,
            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
            if (_PropertyInfo == null)
            {
                throw new ArgumentException(string.Format("{0} is not a property of type {1}.",
                    propertyName, typeof(T)));
            }
        }

        #region IEqualityComparer<T> Members

        public bool Equals(T x, T y)
        {
            object xValue = _PropertyInfo.GetValue(x, null);
            object yValue = _PropertyInfo.GetValue(y, null);

            if (xValue == null)
                return yValue == null;

            return xValue.Equals(yValue);
        }

        public int GetHashCode(T obj)
        {
            object propertyValue = _PropertyInfo.GetValue(obj, null);

            if (propertyValue == null)
                return 0;
            else
                return propertyValue.GetHashCode();
        }

        #endregion
    }
    public class FastPropertyComparer<T> : IEqualityComparer<T>
    {
        private Func<T, Object> getPropertyValueFunc = null;

        /// <summary>
        /// 通过propertyName 获取PropertyInfo对象
        /// </summary>
        /// <param name="propertyName"></param>
        public FastPropertyComparer(string propertyName)
        {
            PropertyInfo _PropertyInfo = typeof(T).GetProperty(propertyName,
            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
            if (_PropertyInfo == null)
            {
                throw new ArgumentException(string.Format("{0} is not a property of type {1}.",
                    propertyName, typeof(T)));
            }

            ParameterExpression expPara = Expression.Parameter(typeof(T), "obj");
            MemberExpression me = Expression.Property(expPara, _PropertyInfo);
            getPropertyValueFunc = Expression.Lambda<Func<T, object>>(me, expPara).Compile();
        }

        #region IEqualityComparer<T> Members

        public bool Equals(T x, T y)
        {
            object xValue = getPropertyValueFunc(x);
            object yValue = getPropertyValueFunc(y);

            if (xValue == null)
                return yValue == null;

            return xValue.Equals(yValue);
        }

        public int GetHashCode(T obj)
        {
            object propertyValue = getPropertyValueFunc(obj);

            if (propertyValue == null)
                return 0;
            else
                return propertyValue.GetHashCode();
        }

        #endregion
    }

    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            ht.Add("mm", "2");
            ht.Add("nn", 22);
            //string ht = "1";
            var str = DAOHelper.CreateCommonDAO().QueryForObject("GetCurrentDateTime", ht);
             
            //Console.Read();
        }

        public static System.Text.Encoding GetFileEncodeType(string filename)
        {
            System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            Byte[] buffer = br.ReadBytes(2);
            if (buffer[0] >= 0xEF)
            {
                if (buffer[0] == 0xEF && buffer[1] == 0xBB)
                {
                    return System.Text.Encoding.UTF8;
                }
                else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
                {
                    return System.Text.Encoding.BigEndianUnicode;
                }
                else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
                {
                    return System.Text.Encoding.Unicode;
                }
                else
                {
                    return System.Text.Encoding.Default;
                }
            }
            else
            {
                return System.Text.Encoding.Default;
            }
        }

        public static string HtmlDecode(string str)
        {
            int length = str.Length;
            str = System.Web.HttpUtility.HtmlDecode(str);
            int newLength = str.Length;

            while (length > newLength)
            {
                length = newLength;
                str = System.Web.HttpUtility.HtmlDecode(str);
                newLength = str.Length;
            }

            StringBuilder sb = new StringBuilder();
            foreach (char ch in str.ToCharArray())
            {
                //如果是普通空格，替换成&nbsp;
                if (ch == 31)
                {
                    sb.Append((char)160);
                }
                else
                {
                    if (!IsControlCharacters(ch))
                        sb.Append(ch);
                }
            }

            return sb.ToString();
        }

        static bool inited = false;
        static List<int> validChars = new List<int>();
        public static void InitValidChar()
        {
            if (inited) return;

            string validCharStr = ConfigurationManager.AppSettings["validChars"];
            if (string.IsNullOrEmpty(validCharStr) || string.IsNullOrEmpty(validCharStr.Trim()))
                return;

            string[] array = validCharStr.Split(',');
            int temp;
            foreach (string item in array)
            {
                if (int.TryParse(item, out temp))
                    validChars.Add(temp);
            }

            inited = true;
        }

        public static bool IsControlCharacters(char ch)
        {
            InitValidChar();
            //如果是tab、回车，则不当作控制字符处理
            if (validChars.Any(p => p == ch))
                return false;

            bool flag1 = ch >= 0 && ch <= 31;
            bool flag2 = ch >= 127 && ch <= 159;

            if (flag1 || flag2)
                return true;
            return false;
        }

    }

    class mmmmm
    {
        public string ss { set; get; }
        public string sds { set; get; }
        public DateTime sss { set; get; }
        public SAA ssds { set; get; }
    }

    public enum SAA
    {
        a,
        g,
        l, i
    }
}
